package com.liaoban.app.ui.mine;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.adapter.RechargeAdapter;
import com.liaoban.app.api.type.PayIndex;
import com.liaoban.app.constants.ExtraParam;
import com.liaoban.app.ui.base.BaseFragment;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ybc on 2018/7/12 0012.
 * 充值
 */
public class RechargeFragment extends BaseFragment {
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private RechargeAdapter mAdapter;
    private int mType;
    private List<PayIndex.MoneyBean> mMoneyBeans;

    public static Bundle getBundle(int type, List<PayIndex.MoneyBean> moneyBeans) {
        Bundle bundle = new Bundle();
        bundle.putInt(ExtraParam.TYPE, type);
        bundle.putSerializable(ExtraParam.LIST, (Serializable) moneyBeans);
        return bundle;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_recharge;
    }

    @Override
    protected void initView(View contentView) {
        initData();
        initRecyclerView();
    }

    private void initData() {
        Bundle bundle = getArguments();//从activity传过来的Bundle
        if (bundle != null) {
            mType = bundle.getInt(ExtraParam.TYPE);
            mMoneyBeans = (List<PayIndex.MoneyBean>) bundle.getSerializable(ExtraParam.LIST);
        }
    }

    protected void initRecyclerView() {
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        rvList.setLayoutManager(manager);
        mAdapter = new RechargeAdapter(mMoneyBeans);
        rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this::itemClick);
    }

    private void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        mAdapter.setSelPos(position);
        PayIndex.MoneyBean moneyBean = mAdapter.getData().get(position);
        RechargeActivity activity = (RechargeActivity) getActivity();
        if (RechargeActivity.TYPE_ALI == mType) {
            activity.mAliMoneyBean = moneyBean;
        } else if (RechargeActivity.TYPE_WX == mType) {
            activity.mWxMoneyBean = moneyBean;
        }
        mAdapter.notifyDataSetChanged();
    }
}
